/*
 * @Description:
 * @Author: chenju
 * @Date: 2021-11-08 15:37:26
 * @LastEditors: chenju
 * @LastEditTime: 2023-01-17 16:11:21
 */
import { get, post } from '@/utils/http'
import type {
  GetRolesListParams,
  GetRoleListResult,
  CreateRoleParams,
  UpdateRoleParams,
  GetMenuTreeResult,
  ChangeUserRoleParams,
  UpdateRoleMenuParams,
  GetRoleUserListResult
} from './model/roleModel'
import type { BasicPagingListParams, BasicPagingListResult } from '@/apis/basicModel'
enum Api {
  // 角色分页列表
  GET_ROLES_LIST = '/upms/customerRole/pageRole',
  // 创建角色
  CREATE_ROLE = '/upms/customerRole/createRole',
  // 编辑角色
  UPDATE_ROLE = '/upms/customerRole/modifyRole',
  // 删除角色
  DELETE_ROLE = '/upms/customerRole/deleteRole',
  // 查询菜单树
  GET_MENU_TREE = '/upms/menu/getCustomerMenu',
  // 编辑角色权限
  UPDATE_ROLE_MENU = '/upms/customerRole/modifyRoleMenu',
  // 角色成员列表
  GET_ROLE_USER_LIST = '/upms/customerRole/listUserByRoleId',
  // 更换用户角色
  CHANGE_USER_ROLE = '/upms/customerRole/changeUserRole',
  // 新增用户角色关联
  ADD_USER_ROLE = '/upms/customerRole/addUserRole',
  // 删除用户角色关联
  DELETE_USER_ROLE = '/upms/customerRole/deleteUserRole',
  // 获取用户menuIds
  GET_MENU_ID = '/upms/role/listRoleMenuIds'
}

// 角色列表（搜索）
export const getRoleList = (
  params: GetRolesListParams & BasicPagingListParams
): Promise<BasicPagingListResult<GetRoleListResult>> => get(Api.GET_ROLES_LIST, params)

// 新增角色
export const createRole = (params: CreateRoleParams): Promise<void> => post(Api.CREATE_ROLE, params)

// 修改角色
export const updateRole = (params: UpdateRoleParams): Promise<void> => post(Api.UPDATE_ROLE, params)

// 删除角色
export const deleteRole = (roleId: number): Promise<void> => post(Api.DELETE_ROLE, { roleId })

// 编辑角色权限
export const updateRoleMenu = (params: UpdateRoleMenuParams): Promise<void> =>
  post(Api.UPDATE_ROLE_MENU, params)

// 角色成员列表
export const getRoleUserList = (
  params: { roleId: number } & BasicPagingListParams
): Promise<BasicPagingListResult<GetRoleUserListResult>> => get(Api.GET_ROLE_USER_LIST, params)

// 更换用户角色
export const changeUserRole = (params: ChangeUserRoleParams): Promise<void> =>
  post(Api.CHANGE_USER_ROLE, params)

// 获取权限树列表
export const getMenuTree = (params: { roleId?: number }): Promise<GetMenuTreeResult[]> =>
  get(Api.GET_MENU_TREE, params)

// 新增用户角色关联
export const postAddUserRole = (roleId: string, userId: string): Promise<void> =>
  post(Api.ADD_USER_ROLE, { roleId, userId })

// 删除用户角色关联
export const postDeleteUserRole = (roleId: string, userId: string): Promise<void> =>
  post(Api.DELETE_USER_ROLE, { roleId, userId })

export const getMenuIds = (roleId: string): Promise<number[]> => get(Api.GET_MENU_ID, { roleId })
